package com.baizhi.service.impl;

import com.baizhi.entity.BzMenu;
import com.baizhi.mapper.BzMenuMapper;
import com.baizhi.service.IBzMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author huiJie
 * @since 2021-08-25
 */
@Service
public class BzMenuServiceImpl extends ServiceImpl<BzMenuMapper, BzMenu> implements IBzMenuService {

    @Override
    public List<BzMenu> showByRank() {
        List<BzMenu> menus = baseMapper.selectList(null);
        List<BzMenu> bzMenus = menus.stream().filter(m -> m.getParentId() == 0).peek((m) -> {
            m.setChilds(showChild(m.getMenuId().intValue(),menus));
        }).collect(Collectors.toList());
        return bzMenus;
    }

    @Override
    public List<BzMenu> showChild(Integer parentId,List<BzMenu> menus) {
        return menus.stream().filter(m->m.getParentId()==parentId.longValue()).peek((m)->{
            m.setChilds(showChild(m.getMenuId().intValue(),menus));
        }).collect(Collectors.toList());
    }

    @Override
    public List<BzMenu> selectPermissionByUsername(String usernmae) {
        return baseMapper.selectPermissionByUsername(usernmae);
    }
}
